//
//  IphoneLinkManController.m
//  PaiQuan
//
//  Created by Mac on 16/7/8.
//  Copyright © 2016年 huigu. All rights reserved.
//

#import "IphoneLinkManController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "ChineseString.h"
#import "IphoneListCellTableViewCell.h"
@interface IphoneLinkManController ()
@property(nonatomic,assign)NSInteger Listcount;
@property(nonatomic,strong)NSMutableArray *nameArr;
@property(nonatomic,strong)NSMutableArray *indexArray;
@property(nonatomic,strong)NSMutableArray *letterResultArr;
@end

@implementation IphoneLinkManController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUPNav];
    self.tableView.backgroundColor = PQ_COLOR_DarkGray;	
    //获取联系人列表
    [self getIphoneList];
    //表头搜索
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, ScreenW, RATIO(108));
    headerView.backgroundColor = PQ_COLOR_DarkGray;
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(RATIO(24), RATIO(18), RATIO(702), RATIO(60))];
    searchBar.backgroundColor =[UIColor whiteColor ];
    
    
    searchBar.backgroundImage = [UIImage imageWithColor:PQ_COLOR_DarkGray frame:searchBar.frame];
    searchBar.placeholder = @"搜索手机联系人";
    [headerView addSubview:searchBar];
    
    self.tableView.tableHeaderView = headerView;
    //联系人分类
    NSArray *stringsToSort =[NSArray arrayWithArray:self.nameArr.copy];
    self.indexArray = [ChineseString IndexArray:stringsToSort];
    self.letterResultArr = [ChineseString LetterSortArray:stringsToSort];
    
}


-(void)getIphoneList{
    
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, NULL);
        
        //3. 获取通讯录的联系人数据
        CFArrayRef peoples = ABAddressBookCopyArrayOfAllPeople(addressbook);
        
        //4. 获取联系人的个数
        CFIndex count = CFArrayGetCount(peoples);
        self.Listcount = count;
        
        for (CFIndex i = 0; i < count; ++i) {
            
            
            ABRecordRef person =  CFArrayGetValueAtIndex(peoples, i);
            
            
            //6.1 获取姓名
            NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
            if (firstName ==nil) {
                firstName = @"";
            }
            
            NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
            if (lastName ==nil) {
                lastName = @"";
            }
            
            NSString *name = [NSString stringWithFormat:@"%@%@",lastName,firstName];
            [self.nameArr addObject:name];
            NSLog(@"%@",name);
            
            //7. 获取电话 ABMultiValueRef --> NSArray
            
            //7.1 获取phone对象
            ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
            
            //7.2 获取phone个数
            //取个数: GetCount
            CFIndex count = ABMultiValueGetCount(phone);
            
            //7.3 遍历数据
            for (CFIndex i = 0; i < count; ++i) {
                
                //7.4 获取数据 --> 标签
                NSString *labelValue = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phone, i));
                NSString *numberValue = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phone, i));
                
            }
            
            //8.  释放CF对象
            CFRelease(phone);
        }
        
        //8. 释放CF对象
        CFRelease(peoples);
        CFRelease(addressbook);
    }
    
    
}

    

-(void)setUPNav{
    self.titleView.title = @"手机联系人";
    [self.titleView.btnBack setImage:PQIMG(@"back") forState:UIControlStateNormal];
    [self.titleView.btnBack setTitle:@"返回" forState:UIControlStateNormal];
    [self.titleView.btnBack setTitleColor:PQ_COLOR_YELLOW forState:UIControlStateNormal];
    
    
}
#pragma mark - Table view data source

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    NSString *key = [self.indexArray objectAtIndex:section];
//    
//    return key;
//}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView =[[UIView alloc] init];
    sectionView.backgroundColor = PQ_COLOR_DarkGray;
    UILabel *letterlabel = [[UILabel alloc] init];
    letterlabel.text = [self.indexArray objectAtIndex:section];
    letterlabel.textColor = [UIColor whiteColor];
    letterlabel.font = FONT(16);
    
    [sectionView addSubview:letterlabel];

    [letterlabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sectionView.left).offset(RATIO(44));
        make.centerY.equalTo(sectionView.centerY);
    }];
    
    return sectionView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return RATIO(60);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.indexArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.letterResultArr objectAtIndex:section] count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"id";
     IphoneListCellTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell =[[IphoneListCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: ID];
        cell.imgView.image = PQIMG(@"05_normal");
        cell.titleLabel.text =  [[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
       
       
        cell.backgroundColor = PQ_COLOR_LightGray;
        
    }
    return cell;
    
    
    
}
-(NSMutableArray*)nameArr{
    if (!_nameArr) {
        _nameArr = [NSMutableArray array];
    }
    
    return _nameArr;
    
    
}
@end
